// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import jQuery from 'jquery'
import MintUi, {Header, Tabbar, TabItem, TabContainer, TabContainerItem } from 'mint-ui'
import App from './App'
import router from './router'
import {EMPLOYEE_KEY} from './assets/js/config'
import LocalCache from './assets/js/cache'
import 'mint-ui/lib/style.css'
import Vee from 'vee-validate'
Vue.use(MintUi, Header.name, Header, Tabbar.name, Tabbar, TabItem.name, TabItem, TabContainer.name, TabContainer, TabContainerItem.name, TabContainerItem)
const veeConfig = {
  locale: 'zh_CN'
}
Vue.use(Vee, veeConfig)
/* eslint-disable no-new */
Vue.config.productionTip = false
const $ = jQuery
window.myVue = new Vue({
  el: '#app',
  router,
  template: '<App/>',
  created () {
    Vue.prototype.post = (opt) => {
      const self = this
      $.ajax({
        url: opt.url,
        method: 'POST',
        data: opt.sendData,
        contentType: 'application/x-www-form-urlencoded;charset=utf-8',
        dataType: 'json',
        timeout: 30000,
        beforeSend (xhr) {
          (function () {
            return typeof opt.beforeSend === 'function' ? opt.beforeSend(xhr) : null
          })()
        },
        success (data) {
          if (typeof data === 'string') {
            data = JSON.parse(data)
          }
          const code = parseInt(data.code)
          if (code === 0) {
            (function () {
              return typeof opt.success === 'function' ? opt.success(data.res.data) : null
            })()
          } else if (code === 101 || code === 102) {
            self.$toast({
              message: '权限已过期',
              duration: 1000
            })
            LocalCache.del(EMPLOYEE_KEY)
            self.$router.push({ path: '/' })
          } else {
            self.$toast({
              message: data.res.msg,
              iconClass: 'icon icon-success',
              duration: 1000
            });
            (function () {
              return typeof opt.failed === 'function' ? opt.failed(data) : null
            })()
          }
        },
        error (x, h, r) {
          self.$toast({
            message: '网络不畅，请稍后重试',
            duration: 2000
          })
        }
      })
    }
  },
  methods: {
  },
  components: { App }
})
